package com.learn.english.service.impl;

import com.learn.english.mapper.CourseChapterMapper;
import com.learn.english.model.dto.CourseChapterDTO;
import com.learn.english.model.entity.CourseChapter;
import com.learn.english.service.ICourseChapterService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author TISNIW
 * @version 1.0
 * @desc
 */
@Service
public class CourseChapterServiceImpl implements ICourseChapterService {

    @Autowired
    private CourseChapterMapper courseChapterMapper;

    @Override
    public List<CourseChapter> getChapterListByCourseId(Integer courseId) {
        return courseChapterMapper.findByCourseId(courseId);
    }

    @Override
    
    public void addChapter(CourseChapterDTO dto) {
        CourseChapter chapter = new CourseChapter();
        BeanUtils.copyProperties(dto, chapter);
        // 设置默认值
        if (chapter.getSort() == null) {
            chapter.setSort(0);
        }
        if (chapter.getIsPublished() == null) {
            chapter.setIsPublished(false);
        }
        courseChapterMapper.insertChapter(chapter);
    }

    @Override
    
    public boolean updateChapter(CourseChapterDTO dto) {
        if (dto == null || dto.getId() == null) {
            return false;
        }
        CourseChapter chapter = courseChapterMapper.findById(dto.getId());
        if (chapter != null) {
            BeanUtils.copyProperties(dto, chapter);
            chapter.setUpdatedAt(new Date());
            courseChapterMapper.updateChapter(chapter);
            return true;
        }
        return false;
    }

    @Override
    
    public void deleteChapter(Long id) {
        courseChapterMapper.deleteChapter(id);
    }

    @Override
    
    public void batchDeleteChapters(List<Long> ids) {
        for (Long id : ids) {
            courseChapterMapper.deleteChapter(id);
        }
    }

    @Override
    
    public void updateChapterSort(List<CourseChapterDTO> chapters) {
        for (CourseChapterDTO dto : chapters) {
            CourseChapter chapter = courseChapterMapper.findById(dto.getId());
            if (chapter != null) {
                chapter.setSort(dto.getSort());
                chapter.setUpdatedAt(new Date());
                courseChapterMapper.updateChapter(chapter);
            }
        }
    }

    @Override
    
    public void publishChapter(Long id, Boolean isPublished) {
        courseChapterMapper.updatePublished(id, isPublished);
    }

    @Override
    public CourseChapter getById(Long id) {
        return courseChapterMapper.findById(id);
    }

    @Override
    public List<CourseChapter> getChaptersByCourseId(Long courseId) {
        if (courseId == null) {
            return List.of();
        }
        // 注意：Mapper方法接受Integer类型，需要转换
        return courseChapterMapper.findByCourseId(courseId.intValue());
    }

    @Override
    public boolean removeById(Long id) {
        if (id == null) {
            return false;
        }
        try {
            // 调用现有的deleteChapter方法来实现删除功能
            deleteChapter(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean removeBatchByIds(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }
        try {
            // 调用现有的batchDeleteChapters方法来实现批量删除功能
            batchDeleteChapters(ids);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean saveChapter(CourseChapterDTO chapterDTO) {
        if (chapterDTO == null) {
            return false;
        }
        try {
            // 直接调用现有的addChapter方法来实现添加章节的功能
            addChapter(chapterDTO);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
